package com.kx.center.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kx.center.entity.Address;
import com.kx.center.enumerate.MessageCode;
import com.kx.center.service.AddressService;
import com.kx.center.util.Response;

/**
 * 收货地址控制器
 * @description 
 * @version 1.0
 * @author 李梦龙 
 * @date 2016年4月26日 下午2:46:17
 */
@Controller
@RequestMapping("/address")
public class AddressController {
	
	@Autowired
	AddressService addressService;

	/**
	 * 前端app添加收货地址
	 * @description 
	 * @version 1.0
	 * @author 李梦龙 
	 * @date 2016年4月26日 下午2:48:11
	 * @param request
	 * @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
	@ResponseBody
	public String addAddress(HttpServletRequest request){
		
		
		Integer accountId = NumberUtils.toInt(request.getParameter("accountId"));
		String username = request.getParameter("username");	
		String tel = request.getParameter("tel");
		String idCard = request.getParameter("idcard");
		String city = request.getParameter("city");
		String cityDetail = request.getParameter("address_detail");
		
		if(accountId <=0 || username == null || "".equals(username) || tel == null || "".equals(tel)
				|| idCard == null || "".equals(idCard) || city == null || "".equals(city)
				|| cityDetail == null || "".equals(cityDetail)){
			return Response.error(MessageCode.PARAMETER_WRONG);
		}
		Address address = new Address();
		address.setAccountId(accountId);
		address.setUsername(username);
		address.setTel(tel);
		address.setIdCard(idCard);
		address.setCity(city);
		address.setCityDetail(cityDetail);
		
		try {
			addressService.addAddress(address);
		} catch (Exception e) {
			e.printStackTrace();
			return Response.error(MessageCode.SERVER_ERROR);
		}
		
		return Response.success();
	}
	
	
	/**
	 * @description 根据用户id获取收货地址信息
	 * @version 1.0
	 * @author 李梦龙 
	 * @date 2016年4月26日 下午4:02:44
	 * @param request
	 * @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/addressList/{accountId}", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getAddressListByAccount(HttpServletRequest request ,@PathVariable("accountId") String accountId){
		
		if(!NumberUtils.isDigits(accountId)){
			return Response.error(MessageCode.PARAMETER_WRONG);
		}
		try {
			//根据用户id查询地址信息
			List<Address> addressList = addressService.getAddressListByAccount(NumberUtils.toInt(accountId));
			
			HashMap<String, Object> result = new HashMap<>();
			result.put("data", addressList);
			
			return Response.success(result);
		} catch (Exception e) {
			e.printStackTrace();
			
		}
		
		return Response.error(MessageCode.SERVER_ERROR);
		
	}
	
	/**
	 * @description 删除收货地址(修改)
	 * @version 1.0
	 * @author 李梦龙 
	 * @date 2016年4月26日 下午4:28:10
	 * @param request
	 * @param accountId
	 * @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/deleteAddress/{id}", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
	@ResponseBody
	public String deleteAddress(HttpServletRequest request ,@PathVariable("id") String id){
		
		if(!NumberUtils.isDigits(id)){
			return Response.error(MessageCode.PARAMETER_WRONG);
		}
		try {
			//根据用户id查询地址信息
			addressService.deleteAddressById(NumberUtils.toInt(id));
			
			return Response.success();
		} catch (Exception e) {
			e.printStackTrace();
			
		}
		
		return Response.error(MessageCode.SERVER_ERROR);
		
	}
	
}








